package cn.com.greatwall.kafka.rest;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.greatwall.common.utils.SecurityUtils;
import cn.com.greatwall.common.utils.StringUtils;
import cn.com.greatwall.kafka.service.ClusterService;
import cn.com.greatwall.kafka.service.MonitorService;
import cn.com.greatwall.kafka.service.dto.ClusterDto;
import cn.com.greatwall.kafka.service.dto.MonitorTopicDto;
import cn.com.greatwall.kafka.service.dto.criteria.MonitorQueryCrieria;
import cn.com.greatwall.logging.annotation.Log;
import cn.com.greatwall.system.modules.system.service.dto.UserDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @Author herw
 * @Time 2021-05-10 14:29:13
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
@RestController(value = "mq-monitorController")
@RequiredArgsConstructor
@Api(tags = "MQ：Monitor管理")
@RequestMapping("/api/mq/monitor")
public class MonitorController {
    private final ClusterService clusterService;
    private final MonitorService monitorService;

    @Log("获取topic")
    @ApiOperation("获取topic")
    @GetMapping("/topic")
    @PreAuthorize("@el.check('topic:list')")
    public ResponseEntity<Object> getTopicList(MonitorQueryCrieria crieria) {
        List<ClusterDto> clusterDtos = StringUtils.isEmpty(crieria.getClusterId()) ? clusterService.queryAll()
                : new ArrayList<ClusterDto>() {
                    {
                        add(clusterService.findById(Long.parseUnsignedLong(crieria.getClusterId())));
                    }
                };
        UserDetails user = SecurityUtils.getCurrentUser();
        return new ResponseEntity<>(monitorService.getTopicList(clusterDtos, user), HttpStatus.OK);
    }
}
